package cn.tedu.autoshow.website.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.website.mapper.UserMapper;
import cn.tedu.autoshow.website.pojo.entity.User;
import cn.tedu.autoshow.website.pojo.param.UserAddNewParam;
import cn.tedu.autoshow.website.pojo.param.UserLoginParam;
import cn.tedu.autoshow.website.pojo.param.UserRegParam;
import cn.tedu.autoshow.website.pojo.param.UserUpdateInfoParam;
import cn.tedu.autoshow.website.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceimpl implements UserService {
    @Autowired
    UserMapper mapper;

    @Override
    public void addNew(UserAddNewParam UserAddNewParam) {
        log.info("【开始】:【新增—账户】");
        log.debug("传入参数, UserAddNewParam={}", UserAddNewParam);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", UserAddNewParam.getUsername()); // name='参数中的相册名称'
        int countByName = mapper.selectCount(queryWrapper);
        log.debug("根据用户名统计匹配的用户数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "添加用户失败，以重复！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        User user=new User();
        BeanUtils.copyProperties(UserAddNewParam, user);

        log.debug("整合实体, user={}", user);
        int insert = mapper.insert(user);
        log.info("【结果1】:插入数据结果, insert = {}", insert);
    }

    @Override
    public void deleteById(Long id) {
        log.info("【开始】:【根据ID删除—用户】");
        log.debug("传入参数, id={}", id);
        int delete = mapper.deleteById(id);
        log.info("【结果1】:删除数据结果, delete = {}", delete);
    }

    @Override
    public void updateInfoById(Long id, UserUpdateInfoParam userUpdateInfoParam) {
        log.info("【开始】:【根据ID更新—用户】");
        log.debug("传入参数, id={}, userUpdateInfoParam={}", id, userUpdateInfoParam);
        User user=new User();
        BeanUtils.copyProperties(userUpdateInfoParam, user);
        user.setId(id);

        log.debug("整合实体, user={}", user);
        int update = mapper.updateById(user);
        log.info("【结果1】:更新数据结果, update = {}", update);
    }
    
    @Override
    public void login(UserLoginParam userLoginParam) {
        
    }

    @Override
    public void reg(UserRegParam userRegParam) {

    }
}
